<template>
  <view>
    <!-- 轮播图区域 -->
    <view class="swiper-box">
      <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
        <swiper-item v-for="(item,index) in detail.pics" :key="index">
          <view class="swiper-item">
            <image @click="preiew(index)" :src="item.pics_big"></image>
          </view>
        </swiper-item>
      </swiper>
    </view>
    <!-- 商品信息 -->
    <view class="detail-info" v-if="detail.goods_name">
      <view class="price">
        ￥{{detail.goods_price}}
      </view>
      <view class="text">
        <view class="left">
          {{detail.goods_name}}
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          <view>收藏</view>
        </view>
      </view>
      <view class="info">
        快递：免运费
      </view>
    </view>
    <view class="detail-text">
      <rich-text :nodes="detail.goods_introduce"></rich-text>
    </view>
    <uni-goods-nav style="position: fixed;bottom:0;width: 100%;border: 1px solid black;" @buttonClick="buttonClick"
      :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" />
  </view>
</template>

<script>
  export default {
    data() {
      return {
        count: 0,
        detail: {},
        options: [{
          icon: 'shop',
          text: '店铺',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: this.$store.state.count

        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    methods: {
      async getDetail(id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: id
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.detail = res.message
      },
      preiew(index) {
        uni.previewImage({
          current: index,
          urls: this.detail.pics.map(item => item.pics_big)
        })
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          this.$store.commit('addCart', {
            goods_id: this.detail.goods_id,
            goods_name: this.detail.goods_name,
            goods_price: this.detail.goods_price,
            goods_count: 1,
            goods_small_logo: this.detail.goods_small_logo,
            goods_state: true
          })
          uni.$showMsg("加入购物车成功")
          this.$store.commit('changeCount')
          this.count = this.$store.state.count
        }
      }
    },
    onLoad(option) {
      this.$store.commit('changeCount')
      this.count = this.$store.state.count
      this.getDetail(option.goods_id)
    },
    watch: {
      count(value) {
        this.options[1].info = value
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 750rpx;
    }
  }

  .detail-info {
    margin-top: 20rpx;
    padding: 10rpx;

    .price {
      color: darkred;
      font-size: 16px;
      font-weight: 900;
    }

    .text {
      margin-top: 20rpx;
      font-size: 14px;
      display: flex;

      .left {
        flex: 5;
        border-right: 1px solid #ccc;
        padding-right: 20rpx;
      }

      .right {
        padding-top: 20rpx;
        flex: 1;
        text-align: center;
        font-size: 12px;
        color: #999;
      }
    }

    .info {
      margin-top: 20rpx;
      color: #999;
      font-size: 12px;
    }
  }

  .detail-text {
    padding-bottom: 100rpx;

    image {
      display: block;
    }
  }
</style>